package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.annotation.AutoFill;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.enumeration.OperationType;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.exception.SetmealEnableFailedException;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class SetmealServicelmpl implements SetmealService {
    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper;
    @Autowired
    private DishMapper dishMapper;

    /**
     * 新增套餐的同时需要保存套餐和菜品的关联关系
     * @param setmealDTO
     */
    @Override
    @Transactional
    public void insertSetMeal(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        //向套餐表插入数据
        setmealMapper.insertSetMeal(setmeal);
        //通过insert标签回显的数据完成对套餐id获取
        Long setmealId11 = setmeal.getId();
        //将获取的套餐id存入到这个中间表中
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        setmealDishes.forEach(setmealDish ->setmealDish.setSetmealId(setmealId11) );
        //保存套餐和菜品的关联关系
        setmealDishMapper.insertBatch(setmealDishes);
    }

    @Override
    public PageResult selectAll(SetmealPageQueryDTO setmealPageQueryDTO) {
        PageHelper.startPage(setmealPageQueryDTO.getPage(),setmealPageQueryDTO.getPageSize());
        Page<SetmealVO> setmealVOS =setmealMapper.selectAll(setmealPageQueryDTO);
        return new PageResult(setmealVOS.getTotal(),setmealVOS.getResult());
    }

    @Override
    public void deleteSetMeal(List<Long> ids) {
        ids.forEach(id->{
            Setmeal setmeal=setmealMapper.getByid(id);
            if (setmeal.getStatus()== StatusConstant.ENABLE){
                throw new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);
            }
        });
        for (Long id : ids) {
            setmealMapper.deleteSetMeal(id);
            setmealDishMapper.deletesetmealDish(id);
        }
    }

    @Override
    public void update(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        setmealMapper.update11(setmeal);
        //获取传过来的setmealDTO id
        Long setmealId = setmealDTO.getId();
        //删除掉河修改相关的中间表存的菜品,就是删除套餐和菜品的关系
        setmealDishMapper.deletesetmealDish(setmealId);
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        //菜品和套餐的集合中设置id
        setmealDishes.forEach(setmealDish -> {setmealDish.setSetmealId(setmealId);});
        //重新插入菜品和套餐关系集合
        setmealDishMapper.insertBatch(setmealDishes);
    }

    @Override
    public SetmealVO selectById11(Long id) {
          SetmealVO setmealVO =setmealMapper.selectById11(id);
        return setmealVO;
    }

    @Override
    public void staterStop(Integer status, Long id) {
        //起售套餐时,判断套餐内是否有停售菜品,有停售菜品提示"套餐内包含未售菜品,无法起售
        if (status==StatusConstant.ENABLE){
            //通过id查询出来的dish内容
            List<Dish> dishList = dishMapper.getBySetmealId(id);
            //如果查询到的id内容不是空就遍历
            if (dishList!=null&&dishList.size()>0){
                //如果遍历出是起售状态则抛出异常不能起售
                dishList.forEach(dish -> {if (StatusConstant.DISABLE==dish.getStatus())
                {throw new SetmealEnableFailedException(MessageConstant.SETMEAL_ENABLE_FAILED);
                }});
            }
        }
        //将要改的id起售状态的封装到套餐对象
        Setmeal setmeal=Setmeal.builder().id(id).status(status).build();
        //然后通过传递的套餐对象修改起售状态
        setmealMapper.update11(setmeal);

    }

    @Override
    public List<Setmeal> selectUerSetmeal(Setmeal setmeal) {
        List<Setmeal> setmeal1 = setmealMapper.selectUerSetmeal(setmeal);
        return setmeal1;
    }

    @Override
    public List<DishItemVO> selectUserSetmealDish(Long id) {
        List<DishItemVO> setmealDishList=setmealMapper.selectUserSetmealDish(id);
        return setmealDishList;
    }


}
